/**
 * Core script to handle the entire layout and base functions sidebar-toggler
 * 核心脚本处理整个布局和基本功能
 */
var App = function() {
	// IE mode
	var isRTL = false;
	var isIE8 = false;
	var isIE9 = false;
	var isIE10 = false;
	var sidebarWidth = 225;
	var sidebarCollapsedWidth = 35;
	var responsiveHandlers = [];
	var handleInit = function() { // 初始化处理
		if ($('body').css('direction') === 'rtl') {
			isRTL = true;
		}
	};
	var handleSidebarState = function() {
		// 删除侧边栏toggler如果窗口宽度小于980
		if ($(window).width() < 980) {
			$('body').removeClass('page-sidebar-closed');
		}
	};
	var handleResponsiveOnResize = function() {
		// 可删除，不确定是否为处理浏览器兼容
		var resize;
		if (isIE8) {
			var currheight;
			$(window).resize(function() {
				if (currheight == document.documentElement.clientHeight) {
					return; // quite event since only body resized not window.
					// 非常事件因为只有body大小不是窗口。
				}
				if (resize) {
					clearTimeout(resize);
				}
				resize = setTimeout(function() {
					handleResponsive();
				}, 50); // wait 50ms until window resize finishes.
				// 等到50毫秒窗口大小调整完成。
				currheight = document.documentElement.clientHeight; // store
				// last body
				// client
				// height
				// 存储上次登录页面的高度
			});
		} else {
			$(window).resize(function() {
				if (resize) {
					clearTimeout(resize);
				}
				resize = setTimeout(function() {
					console.log('resize');
					handleResponsive();
				}, 50); // wait 50ms until window resize finishes.
				// 等到50毫秒窗口大小调整完成。
			});
		}
	};
	// * BEGIN:CORE HANDLERS 开始:核心处理程序 *//
	// this function handles responsive layout on screen size resize or mobile
	// device rotate.
	// 这个函数处理响应布局屏幕尺寸大小或移动设备上旋转。

	var handleSidebarAndContentHeight = function() {
	};
	var handleSidebarMenu = function() {
		// 二级导航下拉
		jQuery('.page-sidebar').on(
				'click',
				'li > a',
				function(e) {
					if ($(this).next().hasClass('sub-menu') == false) {
						if ($('.btn-navbar').hasClass('collapsed') == false) {
							$('.btn-navbar').click();
						}
						return;
					}
					var parent = $(this).parent().parent();
					parent.children('li.open').children('a').children('.arrow')
							.removeClass('open');
					parent.children('li.open').children('.sub-menu').slideUp(
							200);
					parent.children('li.open').removeClass('open');
					var sub = jQuery(this).next();
					if (sub.is(':visible')) {
						jQuery('.arrow', jQuery(this)).removeClass('open');
						jQuery(this).parent().removeClass('open');
						sub.slideUp(200, function() {
							handleSidebarAndContentHeight();
						});
					} else {
						jQuery('.arrow', jQuery(this)).addClass('open');
						jQuery(this).parent().addClass('open');
						sub.slideDown(200, function() {
							handleSidebarAndContentHeight();
						});
					}
					e.preventDefault();
				});
	};
	var handleSidebarToggler = function() {
		// 处理导航栏显示/隐藏
		$('.page-sidebar').on(
				'click',
				'.sidebar-toggler',
				function(e) {
					var body = $('body');
					var sidebar = $('.page-sidebar');
					if ((body.hasClass('page-sidebar-hover-on') && body
							.hasClass('page-sidebar-fixed'))
							|| sidebar.hasClass('page-sidebar-hovering')) {
						body.removeClass('page-sidebar-hover-on');
						sidebar.css('width', '').hide().show();
						e.stopPropagation();
						runResponsiveHandlers();
						return;
					}
					$('.sidebar-search', sidebar).removeClass('open');
					if (body.hasClass('page-sidebar-closed')) {
						body.removeClass('page-sidebar-closed');
						if (body.hasClass('page-sidebar-fixed')) {
							sidebar.css('width', '');
						}
					} else {
						body.addClass('page-sidebar-closed');
					}
					runResponsiveHandlers();
				});
		// handle the search submit 处理搜索提交
		$('.sidebar-search .submit').on('click', function(e) {
			e.preventDefault();
			if ($('body').hasClass('page-sidebar-closed')) {
				if ($('.sidebar-search').hasClass('open') == false) {
					if ($('.page-sidebar-fixed').size() === 1) {
						$('.page-sidebar .sidebar-toggler').click(); // trigger
						// sidebar
						// toggle
						// button
						// 触发栏切换按钮
					}
					$('.sidebar-search').addClass('open');
				} else {
					window.location.href = 'extra_search.html';
				}
			} else {
				window.location.href = 'extra_search.html';
			}
		});
	};
	var handleTooltips = function() {
		if (App.isTouchDevice()) { // if touch device, some tooltips can be
			// skipped in order to not conflict with
			// click events
			// 如果触摸设备,工具提示可以跳过一些为了不与单击事件发生冲突
			jQuery('.tooltips:not(.no-tooltip-on-touch-device)').tooltip();
		} else {
			jQuery('.tooltips').tooltip();
		}
	};
	var handleFixInputPlaceholderForIE = function() {
		// fix html5 placeholder attribute for ie7 & ie8 修复html5 ie7和ie8占位符属性
		if (isIE8 || isIE9) { // ie7&ie8
			// this is html5 placeholder fix for inputs, inputs with
			// placeholder-no-fix class will be skipped(e.g: we need this for
			// password
			// fields)这是html5占位符解决输入,输入与placeholder-no-fix类将被忽略(例如:我们需要这个密码字段)
			jQuery(
					'input[placeholder]:not(.placeholder-no-fix), textarea[placeholder]:not(.placeholder-no-fix)')
					.each(
							function() {
								var input = jQuery(this);
								if (input.val() == ''
										&& input.attr('placeholder') != '') {
									input.addClass('placeholder').val(
											input.attr('placeholder'));
								}
								input.focus(function() {
									if (input.val() == input
											.attr('placeholder')) {
										input.val('');
									}
								});
								input.blur(function() {
									if (input.val() == ''
											|| input.val() == input
													.attr('placeholder')) {
										input.val(input.attr('placeholder'));
									}
								});
							});
		}
	};
	// * END:CORE HANDLERS 结束:核心处理程序 *//

	return {
		// main function to initiate template pages 主要功能启动模板页面
		init : function() {
			handleSidebarMenu(); // handles main menu 处理主菜单 二级导航下拉
			// handleHorizontalMenu(); // handles horizontal menu 处理横向菜单
			handleSidebarToggler(); // handles sidebar hide/show 处理栏隐藏/显示
			handleFixInputPlaceholderForIE(); // fixes/enables html5
			// placeholder attribute for
			// IE9, IE8
			// 修复/支持html5的占位符属性IE9,IE8
		},
		// check for device touch support 检查设备联系支持
		isTouchDevice : function() {
			try {
				document.createEvent('TouchEvent');
				return true;
			} catch (e) {
				return false;
			}
		},
		isIE8 : function() {
			return isIE8;
		},
		isRTL : function() {
			return isRTL;
		},
		getLayoutColorCode : function(name) {
			if (layoutColorCodes[name]) {
				return layoutColorCodes[name];
			} else {
				return '';
			}
		}
	};
}();
// 右上角用户操作显示隐藏
$(document).ready(function() {
	$("a.dropdown-toggle").click(function(e) {
		var display = $("ul.dropdown-menu").css("display");
		if (display == "block") {
			$("ul.dropdown-menu").addClass("hideMenu");
		} else {
			e.stopPropagation();
			$("ul.dropdown-menu").removeClass("hideMenu");
		}
	});
	$(document).click(function() {
		if (!$("ul.dropdown-menu").hasClass("hideMenu")) {
			$("ul.dropdown-menu").addClass("hideMenu");
		}
	});
	$("a.collapsed.btn-navbar").click(function() {
		$("div.nav-collapse.collapse.page-sidebar").slideToggle("slow");
	});
});

// 回到顶部
$('span.go-top').click(function() {
	$('html, body').animate({
		scrollTop : 0
	}, 'slow');
});
// 高级查询
$(function() {
	$("div.list-page-content-top-right label a.advancedQuery").click(
			function() {
				$("div.advancedQueryInfo").slideDown();
			});
});
function hideadvancedQueryBox() {
	$("div.advancedQueryInfo").slideUp();

}
// 选项卡
$(function() {
	var tabContainers = $('div.taskTabs > div.task');
	$('div.taskTabs ul.taskOptions a').click(function() {
		tabContainers.hide().filter(this.hash).show();
		$('div.taskTabs ul.taskOptions a').removeClass('selected');
		$(this).addClass('selected');
		var name = $(this).attr("name");
		if (name == "horizontalTab") {
			horizontalTab();
		} else if (name == "horizontalTab2") {
			horizontalTab2();
		} else if (name == "horizontalTab3") {
			horizontalTab3();
		} else if (name == "horizontalTab4") {
			horizontalTab4();
		} else if (name == "verticalTab") {
			verticalTab();
		}
		return false;
	}).filter(':first').click();
	$("div.taskTabs >ul.taskOptions a.tabs").click(function() {
		$("div.taskTabs >ul.taskOptions a.tabsView").hide();
	});
	$("div.taskTabs >ul.taskOptions a.tabsView").click(function() {
		$("div.taskTabs >ul.taskOptions a.tabsView").hide();
		$(this).show();
	});
});
/**
 * 辅助选项卡
 */
function taskHelpClick(n) {
	if (n == null) {
		n = 0;
	}
	$("div.taskTabs >ul.taskOptions a.tabsView").eq(n).click();
}
/**
 * 列表选项卡
 * 
 * @param n
 */
function taskListClick(n) {
	if (n == null) {
		n = 0;
	}
	$("div.taskTabs >ul.taskOptions a.tabs").eq(n).click();
}
/* 水平二级选项卡 */
function horizontalTab() {
	var tabContainersInfo = $('div.horizontalMenu > div.timeType');
	$('div.horizontalMenu ul.taskOptionsInfo a').click(function() {
		var width = $(this).width();
		var aWidth = (width + 60 - 8) / 2;
		$(this).find("div.triangle").css("left", aWidth);
		tabContainersInfo.hide().filter(this.hash).show();
		$('div.horizontalMenu ul.taskOptionsInfo a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
};
function horizontalTab2() {
	var tabContainersInfo = $('div.horizontalMenu2 > div.timeType');
	$('div.horizontalMenu2 ul.taskOptionsInfo a').click(function() {
		var width = $(this).width();
		var aWidth = (width + 60 - 8) / 2;
		$(this).find("div.triangle").css("left", aWidth);
		tabContainersInfo.hide().filter(this.hash).show();
		$('div.horizontalMenu2 ul.taskOptionsInfo a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
};
function horizontalTab3() {
	var tabContainersInfo = $('div.horizontalMenu3 > div.timeType');
	$('div.horizontalMenu3 ul.taskOptionsInfo a').click(function() {
		var width = $(this).width();
		var aWidth = (width + 60 - 8) / 2;
		$(this).find("div.triangle").css("left", aWidth);
		tabContainersInfo.hide().filter(this.hash).show();
		$('div.horizontalMenu3 ul.taskOptionsInfo a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
};
function horizontalTab4() {
	var tabContainersInfo = $('div.horizontalMenu4 > div.timeType');
	$('div.horizontalMenu4 ul.taskOptionsInfo a').click(function() {
		var width = $(this).width();
		var aWidth = (width + 60 - 8) / 2;
		$(this).find("div.triangle").css("left", aWidth);
		tabContainersInfo.hide().filter(this.hash).show();
		$('div.horizontalMenu4 ul.taskOptionsInfo a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
};
/* 垂直二级选项卡(一级选项卡下面的二级选项卡) */
function verticalTab() {
	var tabContainersInfo = $('div.verticalMenu > div.timeType');
	$('div.verticalMenu ul.taskOptionsInfo a').click(function() {
		tabContainersInfo.hide().filter(this.hash).show();
		$('div.verticalMenu ul.taskOptionsInfo a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
};
// 显示隐藏内容框
$(function() {
	$("div.content-box .content-box-title p a").click(
			function() {
				$(this).parents(".content-box-title").next(
						".content-box-content").toggle();
				$(this).find("i").toggleClass("icon-chevron-down");
			});
});
// 全选
$(function() {
	$("input#checkall").click(
			function() {
				$(this).parents(".list-page-content-table").find("td")
						.children("input[type='checkbox']").attr("checked",
								this.checked);
			});
});
/* 数据框显示隐藏 */
$(document).ready(
		function() {
			$("p.titleRight i.toggleBox").click(
					function() {
						var display = $(this).parents('.editPageTitle').next(
								'.editPageInfo').css('display');
						if (display == 'block') {
							$(this).parents('.editPageTitle').next(
									'.editPageInfo').hide();
							$(this).removeClass('icon-chevron-up').addClass(
									'icon-chevron-down');
						} else {
							$(this).parents('.editPageTitle').next(
									'.editPageInfo').show();
							$(this).removeClass('icon-chevron-down').addClass(
									'icon-chevron-up');
						}
					});
		});
$(function() {
	$("a.btn.showEditBox").click(
			function() {
				$(this).parents('.list-page-content-top').parent('.showOrHide')
						.hide();
				$(this).parents('.list-page-content-top').parent('.showOrHide')
						.next('.hideOrShow').show();
			});
	$("a.btn.hideEditBox").click(
			function() {
				$(this).parents('.editPage').parent('.hideOrShow').hide();
				$(this).parents('.editPage').parents('.timeType').children(
						'.showOrHide').show();
			});
});
/*
 * 人员选择
 */
$(function() {
	/* 人员选择框显示隐藏 按钮 */
	$("div.controls a.showUserInfoBox").click(function() {
		var width = $(window).width();
		var height = $(window).height();
		$("#fullbg").css("width", width + 20);
		$("#fullbg").css("height", height);
		$("#fullbg").show();
		$("div.userInfoBox").show();
		$("body").css("overflow", "hidden");

	});
	$("div.controls a.showdepInfoBox").click(function() {
		var width = $(window).width();
		var height = $(window).height();
		$("#fullbg").css("width", width + 20);
		$("#fullbg").css("height", height);
		$("#fullbg").show();
		$("div.depInfoBox").show();
		$("body").css("overflow", "hidden");
	});

});
/*
 * 人员选择框显示隐藏 表格
 */
function showDishesMenuBox() {
	var width = $(window).width();
	var height = $(window).height();
	$("#fullbg").css("width", width);
	$("#fullbg").css("height", height);
	$("#fullbg").show();
	$("div.dishesMenuBox").show();
};
/* 删除已选人员 */
function emptyP() {
	$("div.selectedDataInfo p i.icon-remove").click(function() {
		$(this).parents("p").remove();
	});
}
/**
 * 显示提示框
 */
function alertBox(mesg, next) {
	var flag = false;
	if (mesg == null) {// 设置默认空消息
		mesg = "";
	}
	if (next == null || next == "") {// 是否是确认框
		$("#batchDeleteBox .Determine").hide();
	} else {
		$("#batchDeleteBox .Determine").show();
		$("#batchDeleteBox .Determine").bind("click", next);

	}
	$("#batchDeleteBox .contentInfo").html(mesg);// 设置消息
	$("#batchDeleteBox").show();// 显示提示框
	return flag;
}
/**
 * 关闭提示框
 */
function alertBoxClose() {// 关闭提示框
	$("#batchDeleteBox").hide();
}
/**
 * 关闭导入框
 */
function impBoxClose() {
	$(".PopupBox").hide();
}
/** ********ajax遮罩层*************** */
$(document).ajaxStart(function() {
	$('.loadingBox').show();
	var fullbgWidth = $(".right-side-content").width();
	var fullbgHeight = $(".right-side-content").height();
	$("#fullbg").css("width", fullbgWidth);
	$("#fullbg").css("height", fullbgHeight);
	$("#fullbg").show();
}).ajaxStop(function() {
	$('.loadingBox').hide();
	$("#fullbg").css("width", "0px");
	$("#fullbg").css("height", "0px");
	$("#fullbg").hide();
});
/** **********end ajax遮罩层*************** */

/** ***************start导航栏展开效果*************** */
$(function() {
	if ($(".pageName .oneMenu").length > 0) {
		var one = $.trim($(".pageName .oneMenu").html());
		var tow = $.trim($(".pageName .twoMenu").html());
		$(".page-sidebar-menu span.title").each(function() {
			var thisHtml = $(this).html();
			if ($.trim(thisHtml) == one) {
				$(this).parent().parent().click();
				$(".page-sidebar-menu li").removeClass("active");
				$(".page-sidebar-menu li").removeClass("start");
				$(this).parent().parent().addClass("active");
				$(this).parent().parent().addClass("start");
				$(this).next().addClass("open");
				// $(this).next().removeClass("arrow");
				$(this).parent().next().show();

			}
		});
		$(".page-sidebar-menu li").removeClass("towSelected");
		$(".page-sidebar-menu a[title='" + tow + "']").parent().addClass(
				"towSelected");

	}
});

/** *******************end导航栏关闭效果******************************* */
$(function() {
	$("table th input[type='checkbox']").prop("checked", false);// 批量删除复选框默认不选择
	// 动态时间
	setInterval("GetTime()", 1000);
});

function GetTime() {
	var mon, day, now, hour, min, ampm, time, str, tz, end, beg, sec;
	/*
	 * mon = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
	 * "Sep", "Oct", "Nov", "Dec");
	 */
	mon = new Array("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月",
			"10月", "11月", "12月");
	/*
	 * day = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	 */
	day = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
	now = new Date();
	hour = now.getHours();
	min = now.getMinutes();
	sec = now.getSeconds();
	if (hour < 10) {
		hour = "0" + hour;
	}
	if (min < 10) {
		min = "0" + min;
	}
	if (sec < 10) {
		sec = "0" + sec;
	}
	$("#Timer").html(
			"<p class='data1'>" + now.getFullYear() + "年" + mon[now.getMonth()]
					+ "" + now.getDate() + "日</p><p class='data2'>" + hour
					+ ":" + min + ":" + sec + "</p><p class='data3'>"
					+ day[now.getDay()] + "</p>");
	$("#menuTime").html("" + hour + ":" + min + ":" + sec + "");

}
$(function() {
	$("div.note .dataInfo a.editIcon").click(
			function() {
				$(this).parent("div.dataInfo").children("input").attr(
						"disabled", false);
				$(this).parent("div.dataInfo").children("a.deleteIcon")
						.children("i").attr("class", "icon-save");
			});
	$("div.note .dataInfo a.deleteIcon i").click(
			function() {
				$(this).parents("div.dataInfo").children("input").attr(
						"disabled", "disabled");
				$(this).attr("class", "icon-trash");
			});
});